☰ See All Chapters |
Spring @ComponentScan Annotation
@ComponentScan annotation is used to specify packages for spring to scan for annotated components. Spring needs to know which packages contain beans, otherwise you would have to register each bean individually. Hence @ComponentScan annotation is a supporting annotation for @Configuration annotation. Spring instantiate components from specified packages for those classes annotated with @Component, @Controller, @Service, and @Repository.
@ComponentScan Annotation Example 1
Though @ComponentScan annotation is released in version 3.1, with spring 3.1/3.2 dependencies, we may get the error - java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context. If you face this error, we will recommend you to use spring 4.2 dependencies. The easiest solution seems to be for Data/Boot to be more defensive there.
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>Spring3_ComponentScan_Example1</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version>
<properties> <spring.version>4.2.4.RELEASE</spring.version> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
</project> |
Employee.java
package com.java4coding;
import org.springframework.stereotype.Component;
@Component public class Employee{ public void printEmployeeName(String name){ System.out.println(name); } } |
Demo.java
package com.java4coding;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = { "com.java4coding" }) public class Demo { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(Demo.class); context.refresh(); Employee employee = (Employee) context.getBean("employee"); employee.printEmployeeName("Manu Manjunatha"); context.close(); } } |
Project Directory Structure
Output
@ComponentScan Annotation Example 2
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>Spring3_ComponentScan_Example2</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version>
<properties> <spring.version>4.2.4.RELEASE</spring.version> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
</project> |
Employee.java
package com.java4coding;
import org.springframework.stereotype.Component;
@Component public class Employee{ public void printEmployeeName(String name){ System.out.println(name); } } |
EmployeeService.java
package com.java4coding.employeeservice;
import com.java4coding.employee.Employee;
public class EmployeeService { Employee employee;
public EmployeeService (Employee employee) { this.employee = employee; }
public Employee getEmployee(){ return employee; } } |
ContextUtil.java
package com.java4coding.contextutil;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration;
@Configuration public class ContextUtil { private static final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
public static AnnotationConfigApplicationContext getAnnotationConfigApplicationContext() { return context; } }
|
SpringConfiguration.java
package com.java4coding;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.java4coding.contextutil.ContextUtil; import com.java4coding.employee.Employee; import com.java4coding.employeeservice.EmployeeService;
@Configuration @ComponentScan(basePackages= {"com.java4coding.employee"}) public class SpringConfiguration { @Bean() public EmployeeService employeeService() { AnnotationConfigApplicationContext context = ContextUtil.getAnnotationConfigApplicationContext(); return new EmployeeService((Employee)context.getBean("employee")); } } |
Demo.java
package com.java4coding;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.java4coding.contextutil.ContextUtil; import com.java4coding.employeeservice.EmployeeService;
public class Demo { public static void main(String[] args) { AnnotationConfigApplicationContext context = ContextUtil.getAnnotationConfigApplicationContext(); context.register(SpringConfiguration.class); context.refresh(); EmployeeService employeeService = (EmployeeService)context.getBean("employeeService"); employeeService.getEmployee().printEmployeeName("Manu M"); } } |
Project Directory Structure
Output
All Chapters